<?php

	require_once '../utils/SqlHelper.class.php';
	require_once'../utils/common.php';
	session_start();
	$username = $_POST['username'];
	$password = $_POST['password'];
	$userId = 0;
	$ip = $_SERVER['REMOTE_ADDR'];
	$sql = "select id,password from j_tab_sysuser where username = '$username'";
	$sqlHelper = new SqlHelper();
	$res = $sqlHelper->query_row($sql);
	if ($row = $res[0]) {
		if ($row[1] == $password) {
			$userId = $row[0];
			$sql = "update j_tab_sysuser set isLogin = 1 where id = $userId";
			$sqlHelper->update($sql);
			$sql = "insert into j_tab_loginlog values(null,$userId,'$ip',UNIX_TIMESTAMP())";
			$sqlHelper->update($sql);
			$sqlHelper->close_connect();
			$_SESSION['userId']=$userId;
			header("Location: welcome.php?id=$userId");
			exit();
		}
	}
	header("Location: login.php?errorno=1");
	exit();
	
?>